// EditStudentPage.cpp : implementation file
//

#include "stdafx.h"
#include "SelectLesson.h"
#include "EditStudentPage.h"
#include "StudentInformationset.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEditStudentPage property page

IMPLEMENT_DYNCREATE(CEditStudentPage, CPropertyPage)

CEditStudentPage::CEditStudentPage() : CPropertyPage(CEditStudentPage::IDD)
{
	//{{AFX_DATA_INIT(CEditStudentPage)
	m_Sex = FALSE;
	m_StudentName = _T("");
	m_StudentNumber = 0;
	m_Birthday = _T("");
	//}}AFX_DATA_INIT
}

CEditStudentPage::~CEditStudentPage()
{
}
BOOL CEditStudentPage::OnCommand(WPARAM wParam, LPARAM lParam)
{
    SetModified(TRUE);
    return CPropertyPage::OnCommand(wParam, lParam);
}

void CEditStudentPage::DoDataExchange(CDataExchange* pDX)
{
    TRACE("Entering CEditStudentPage::DoDataExchange -- %d\n",
          pDX->m_bSaveAndValidate);

	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEditStudentPage)
	DDX_Control(pDX, IDC_STUDENTNUMBER, m_EditStudentNumber);
	DDX_Control(pDX, IDC_STUDENTNAME, m_EditStudentName);
	DDX_Control(pDX, IDC_SEX, m_EditSex);
	DDX_Control(pDX, IDC_BIRTHDAY, m_EditBirthday);
	DDX_Text(pDX, IDC_SEX, m_Sex);
	DDX_Text(pDX, IDC_STUDENTNAME, m_StudentName);
	DDX_Text(pDX, IDC_STUDENTNUMBER, m_StudentNumber);
	DDX_Text(pDX, IDC_BIRTHDAY, m_Birthday);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEditStudentPage, CPropertyPage)
	//{{AFX_MSG_MAP(CEditStudentPage)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	ON_BN_CLICKED(IDC_EDIT, OnEdit)
	ON_BN_CLICKED(IDC_FIRST, OnFirst)
	ON_BN_CLICKED(IDC_LAST, OnLast)
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	ON_BN_CLICKED(IDC_PREV, OnPrev)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditStudentPage message handlers

BOOL CEditStudentPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	try
	{
		m_database=new CDatabase;
		if(!m_database->Open(_T( "SelectLesson registration" ),FALSE,FALSE, _T( "ODBC;" ),FALSE))
		{
			AfxMessageBox("ODBC Connection Faile!");
			return FALSE;
		}
	    CString strSQL="select * from StudentInformation";
		m_pStudentset=new CStudentInformationset(m_database);
		m_pStudentset->Open(CRecordset::dynaset,strSQL);
        GetValue();
	}
	catch(CDBException* pEx)
	{
		pEx->ReportError();
	} 

	return TRUE;  
}

void CEditStudentPage::OnAdd() 
{
	if(m_pStudentset->CanUpdate()!=0)
	{
		try
		{
			if(!UpdateData(TRUE)) return;
				UpdateData(TRUE);
		    if(m_StudentNumber==0)
			{ 
				MessageBox("m_StudentNumber must be inputted","notice!",\
					MB_ICONINFORMATION|MB_OK);
				return;
			}
			m_pStudentset->AddNew();
			SetValue();
			m_pStudentset->Requery();
			m_pStudentset->MoveLast();
			AfxMessageBox("Add new record succeed");
		}
		catch(CDBException* pEx)
		{
			pEx->ReportError();
		}
	}
	else
	{
		AfxMessageBox("Can't Append New Record");
	}
}

void CEditStudentPage::OnDel() 
{
	if(m_pStudentset->CanUpdate()!=0)
	{    	
		m_pStudentset->CancelUpdate();
	    if(m_pStudentset->IsBOF() && m_pStudentset->IsEOF()) {
			GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
			GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
			GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
			GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
			ClearEditText();
			return;}
		else
			GetValue();
		if(MessageBox("Really want to delete the record y/n?","Delete the record!",MB_YESNO|MB_ICONINFORMATION) == IDYES)
		{
			m_pStudentset->Delete();
//			m_pStudentset->MovePrev();
		if(!UpdateData(TRUE)) return;
			UpdateData(TRUE);
			m_pStudentset->MoveNext();
			try
			{
				if(m_pStudentset->IsEOF()){
					m_pStudentset->MovePrev();}
				else{}
				GetValue();
			}
/*			{
				if(!m_pStudentset->IsEOF()){
					m_pStudentset->MoveNext();}
				else{
					m_pStudentset->MovePrev();}
				GetValue();*/
			catch(CDBException* pEx)
			{
				pEx->ReportError();
			}
		}	
	}
	else
	{
		AfxMessageBox("Can't Append del the Record!");
	}
}

void CEditStudentPage::OnEdit() 
{
	if(m_pStudentset->CanAppend())
	{
//		GetValue();
	   	m_pStudentset->Edit();
		SetValue();
		AfxMessageBox("Edit the Record succeed!");
	}
	else
	{
		AfxMessageBox("Can't Modify the Record");
	}	
}

void CEditStudentPage::OnFirst() 
{
	GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
	GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
	m_pStudentset->MoveFirst();
	m_pStudentset->MoveNext();
	OnPrev();
}
void CEditStudentPage::OnPrev() 
{
	GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
	GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
	m_pStudentset->MovePrev();
	if(m_pStudentset->IsBOF())
	{
		GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
		GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
		m_pStudentset->MoveNext();
		return;
	}
	GetValue();	

}
void CEditStudentPage::OnLast() 
{
	GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
	GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
	m_pStudentset->MoveLast();
	m_pStudentset->MovePrev();
	OnNext();
}

void CEditStudentPage::OnNext() 
{
	GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
	GetDlgItem(IDC_PREV)->EnableWindow(TRUE);
	m_pStudentset->MoveNext();
	if(m_pStudentset->IsEOF())
	{
		GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
		GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
		m_pStudentset->MovePrev();
		return;
	}
	GetValue();
}


void CEditStudentPage::GetValue()
{
	CString strTemp;
	strTemp.Format(_T("%d-%d-%d"),m_pStudentset->m_Birthday.GetYear(),\
			m_pStudentset->m_Birthday.GetMonth(),m_pStudentset->m_Birthday.GetDay());
	m_StudentNumber		= m_pStudentset->m_StudentNumber ; 
	m_StudentName		= m_pStudentset->m_StudentName    ;
	m_Sex				= m_pStudentset->m_Sex; 
	m_Birthday			= strTemp;
	UpdateData(FALSE);

}
void CEditStudentPage::SetValue()
{
	UpdateData(TRUE);
   	int year,month,day;
	year=atoi(m_Birthday.Left(4));
	if(m_Birthday.Mid( 6, 1 ) == _T("-"))
	{
		month=atoi(m_Birthday.Mid( 5, 1 ));
		day=atoi(m_Birthday.Mid( 7, m_Birthday.GetLength()-7));
	}
	else if(m_Birthday.Mid( 7, 1 ) == _T("-"))
	{
		month=atoi(m_Birthday.Mid( 5, 2 ));
		day=atoi(m_Birthday.Mid( 8, m_Birthday.GetLength()-8));
	}
	CTime t(year, month, day,0,0,0,-1);

	m_pStudentset->m_StudentNumber		=   m_StudentNumber;
	m_pStudentset->m_StudentName		=   m_StudentName;
	m_pStudentset->m_Sex				=   m_Sex;
	m_pStudentset->m_Birthday			=   t;
	m_pStudentset->Update();
}


BOOL CEditStudentPage::IsFirstRecord()
{
	if(m_pStudentset->IsBOF()&&m_pStudentset->IsEOF())
	{
		return TRUE;
	}
	else if(m_pStudentset->IsBOF())
	{
		m_pStudentset->MoveFirst();
		return TRUE;
	}
	else
		return FALSE;
}

BOOL CEditStudentPage::IsLastRecord()
{
	if(m_pStudentset->IsBOF()&&m_pStudentset->IsEOF())
	{
		return TRUE;
	}
	else if(m_pStudentset->IsEOF())
	{
		m_pStudentset->MoveLast();
		return TRUE;
	}
	else
		return FALSE;
}

void CEditStudentPage::ClearEditText()
{
	m_StudentName= _T("");
	m_StudentNumber= 0;
	m_Sex= 0;
	m_Birthday=_T("");
	UpdateData(FALSE);
}
